﻿using System.Net;

namespace WebApi.Controllers
{
    /// <summary>
    /// 
    /// </summary>
    /// <typeparam name="T"></typeparam>
    public class ActionResult<T>
    {
        /// <summary>
        /// 状态码
        /// </summary>
        public int Code { get; set; } = (int)HttpStatusCode.OK;

        /// <summary>
        /// 结果说明
        /// </summary>
        public string? Msg { get; set; } = "SUCCESS";

        /// <summary>
        /// 结果
        /// </summary>
        public T Data { get; set; }

        /// <summary>
        /// 返回成功ResultCode=2000 用于单个记录
        /// </summary>
        /// <param name="data">需要返回的数据</param>
        /// <returns></returns>
        public ActionResult<T> Success(T data)
        {
            if (data != null)
            {
                this.Data = data;
            }
            return this;
        }

        /// <summary>
        /// 返回失败(自定义返回)
        /// </summary>
        /// <param name="msg">错误信息</param>
        /// <returns></returns>
        public ActionResult<T> Error(string msg)
        {
            Code = (int)HttpStatusCode.InternalServerError;
            Msg = msg;
            return this;
        }
    }
}
